// import { getAuthentication, getAppInfo } from './util';

export default function appSdk(method, data, callback) {
  // const appInfo = getAppInfo(parseInt(getAuthentication('zfy_packageId'), 10));
  console.log('app 调用信息', method, data)
  const ua = navigator.userAgent.toLowerCase()
  let system = ''
  let rechargeSdk = null
  let appJs = null
  const webkit = window.webkit
  if (/(android)/i.test(ua)) {
    system = 'android'
    // rechargeSdk = window.rechargeSdk;
  } else if (/(iphone|ipad|ipod|ios)/i.test(ua)) {
    system = 'ios'
    // rechargeSdk = window.webkit.messageHandlers.rechargeSdk; // ios 希望这样用
  }
  if (system === 'android') {
    console.log('window.android', window?.android)
    if (!window?.android) {
      console.log('当前网页可能不在 android app 内访问，无法调用 sdk')
      callback({
        status: 'fail',
        message: '当前网页可能不在 android app 内访问，无法调用 sdk'
      })
      return false
    }
    rechargeSdk = window?.android
    // appJs = window.appJs;
  }
  if (system === 'ios') {
    console.log('window.webkit', webkit)
    if (!window.webkit && !window.rechargeSdk) {
      console.log('当前网页不是在 ios app 内访问，无法调用 sdk')
      return false
    }
    // rechargeSdk = window?.amJsBridge
    rechargeSdk = window.webkit ? window.webkit.messageHandlers.amJsBridge : window.rechargeSdk
    // appJs = window.appJs ? webkit.messageHandlers.appJs: window.appJs;
  }
  switch (method) {
    // appstore 只有ios
    case 'appStorePay':
      window.resultCallback = callback
      rechargeSdk.postMessage({
        data, // data => goods id
        sel: 'appStorePay'
      })
      break
    // 微信支付
    case 'wechatPay':
      window.resultCallback = callback
      console.log('微信 window.resultCallback', window?.resultCallback)
      if (system === 'android') {
        rechargeSdk.wechatPay(JSON.stringify(data))
      } else {
        rechargeSdk.postMessage({
          data,
          sel: 'wechatPay'
        })
      }
      break
    // 支付宝支付
    case 'aliPay':
      window.resultCallback = callback
      console.log('支付宝 window.resultCallback', window?.resultCallback)
      if (system === 'android') {
        // string 无需json
        rechargeSdk.aliPay(data)
      } else {
        rechargeSdk.postMessage({
          data,
          sel: 'alipay'
        })
      }
      break
    // 关闭 webview
    case 'closeWebview':
      if (system === 'android') {
        rechargeSdk.webClose()
      } else {
        rechargeSdk.postMessage({
          sel: 'webClose'
        })
      }
      break
    case 'agreeCreateRoomProtocol':
      rechargeSdk.agreeCreateRoomProtocol()
      break
    case 'backPress':
      if (system === 'android') {
        rechargeSdk.backPress()
      } else {
        rechargeSdk.postMessage({
          sel: 'backPress'
        })
      }
      break
    case 'jumpToMain':
      if (system === 'android') {
        rechargeSdk.jumpToMain(data)
      } else {
        rechargeSdk.postMessage({
          data,
          sel: 'jumpToMain'
        })
      }
      break
    case 'activeJumpToRoom':
      if (system === 'android') {
        rechargeSdk.activeJumpToRoom(data)
      } else {
        rechargeSdk.postMessage({
          data,
          sel: 'activeJumpToRoom'
        })
      }
      break
    case 'jumpToRecharge':
      if (system === 'android') {
        rechargeSdk.jumpToRecharge()
      } else {
        rechargeSdk.postMessage({
          data,
          sel: 'jumpToRecharge'
        })
      }
      break
    case 'jumpToRealBank':
      if (system === 'android') {
        rechargeSdk.jumpToRealBank()
      } else {
        rechargeSdk.postMessage({
          sel: 'jumpToRealBank'
        })
      }
      window.resultCallback = callback
      break
    case 'jumpToUserIdentity':
      window.resultCallback = callback
      if (system === 'android') {
        rechargeSdk.jumpToUserIdentity(data)
      } else {
        rechargeSdk.postMessage({
          sel: 'jumpToUserIdentity',
          data
        })
      }
      break
    case 'jumpToRealName':
      window.resultCallback = callback
      if (system === 'android') {
        rechargeSdk.jumpToRealName()
      } else {
        rechargeSdk.postMessage({
          sel: 'jumpToRealName'
        })
      }
      break
    case 'jumpToUnionIncomeDetail':
      if (system === 'android') {
        rechargeSdk.jumpToUnionIncomeDetail(data)
      } else {
        rechargeSdk.postMessage({
          data,
          sel: 'jumpToUnionIncomeDetail'
        })
      }
      break
    case 'setFullScreen':
      if (system === 'android') {
        rechargeSdk.setFullScreen(data)
      } else {
        rechargeSdk.postMessage({
          data,
          sel: 'setFullScreen'
        })
      }
      break
    case 'jumpToAliAuth':
      if (system === 'android') {
        rechargeSdk.jumpToAliAuth(data)
      } else {
        rechargeSdk.postMessage({
          data,
          sel: 'jumpToAliAuth'
        })
      }
      window.resultCallback = callback
      break
    case 'getStatusBarHeight':
      rechargeSdk.postMessage({
        data,
        sel: 'getStatusBarHeight'
      })
      window.resultCallback = callback
      break
    case 'jumpToActivity':
      if (system === 'android') {
        rechargeSdk.jumpToActivity('com.ameng.tec.user.activity.InviteFriendActivity')
      } else {
        rechargeSdk.postMessage({
          data: 'com.ameng.tec.user.activity.InviteFriendActivity',
          sel: 'jumpToActivity'
        })
      }
      break
    case 'updateTitle':
      if (system === 'android') {
        appJs.webTitle(data)
      }
      break
    // 跳转个人信息页
    case 'toPersonCenter':
      // 接口参数：mid
      appJs.toPersonCenter(data)
      break
    // 调用分享  加上了最直播的 ios 分享
    case 'setShareData':
      if (system === 'android') {
        console.log('android传过去的', JSON.stringify(data))
        appJs.setShareData(JSON.stringify(data))
      } else if (system === 'ios') {
        console.log('ios传过去的', data)
        window.setShareData(data)
      }
      break
    case 'onShare':
      // 调用分享
      if (system === 'android') {
        rechargeSdk.onShare()
      } else if (system === 'ios') {
        window.onShare()
      }
      break
    case 'recharge':
      if (system === 'android') {
        rechargeSdk.roomDetail(data)
      } else if (system === 'ios') {
        rechargeSdk.roomDetail(data)
      }
      break
    case 'chatPrice':
      console.log('chatPrice', data)
      if (system === 'android') {
        appJs.chatPrice(data)
      }
      break
    // TEST: 测试 ios ‘recharge’ 方法
    case 'iosRecharge':
      if (system === 'ios') {
        rechargeSdk.postMessage({
          sel: 'iosRecharge'
        }) // ios 希望改成这样调用
        console.log('iosRecharge', rechargeSdk)
        // rechargeSdk.recharge();
      }
      break
    // 右上角自定义标题
    case 'changeMenuButton':
      if (system === 'android') {
        console.log('appJs.changeMenuButton', data)
        appJs.changeMenuButton(data.title, data.url)
      }
      break
    case 'downloadPic':
      // 下载图片 传base64
      if (system === 'android') {
        console.log('appJs.downloadPic')
        appJs.downloadPic(data)
      }
      break
    // 单独分享图片
    case 'onShareImage':
      if (system === 'android') {
        console.log('rechargeSdk.onShareImage')
        rechargeSdk.onShareImage()
      }
      break
    default:
      throw new Error(`method ${method} is not exist.`)
  }
  // }
  return true
}
